package qj.pooling;

import qj.concurrent.Workable;

/**
 * This helper class consists exclusively of static methods that operate on or return object pools.
 * It contains polymorphic algorithms that operate on pools, "wrappers", which return a new pool backed by a specified pool, and a few other odds and ends.
 * <br>
 * The methods of this class all throw a NullPointerException if the pool objects provided to them are null.
 * <br>
 * This class is a member of the QuanLA Pooling Framework. 
 * <br>
 * Created by QuanLA<br>
 * Date: Apr 1, 2006<br>
 * Time: 11:39:11 AM<br>
 */
public class Pools {
	/**
	 * Returns a locked pool that have only accquire and release methods are accessible.
	 * 
	 * @param pool
	 * @return a locked pool
	 */
    public static ObjectPool lockedPool(ObjectPool pool) {
        return new LockedPool(pool);
    }

    /*
     * Simple implementation of Locked Pool.
     * <br>
     * Lock on methods: destroyAllFreeObjects, setPoolCleaner, setPoolManager, setRestrictSize and setScheduledCleaningTime
     * <br>
     */
    private static class LockedPool implements ObjectPool {
        private ObjectPool pool;
        public LockedPool(ObjectPool pool) {
        	if (pool==null)
        		throw new NullPointerException();
            this.pool = pool;
        }

        public Object acquire() {
            return pool.acquire();
        }

        public void release(Object obj) {
            pool.release(obj);
        }

        public int size() {
            return pool.size();
        }

        public void destroyAllFreeObjects() {
            throw new RuntimeException("Operation Not Supported");
        }

        public void setPoolCleaner(Workable poolCleaner) {
            throw new RuntimeException("Operation Not Supported");
        }

        public void setPoolManager(PoolManagable poolManager) {
            throw new RuntimeException("Operation Not Supported");
        }

        public void setRestrictSize(int restrictSize) {
            throw new RuntimeException("Operation Not Supported");
        }

        public void setScheduledCleaningTime(int scheduledCleaningTime) {
            throw new RuntimeException("Operation Not Supported");
        }
    }
}
